<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
/**
 * Description of contacts
 *
 * @author khanhdoan
 */
require APPPATH . '/libraries/REST_Controller.php';

class Contacts_api extends REST_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model("contacts_m");
    }

    public function insert() {
        
        $args = array('first_name', 'last_name', 'email', 'phone', 'birth_day', 'description', 'contact_code', 'sex', 'address', 'tax_code');
        $args = $this->_parse_post_args($args);
        //Validation
        $rules = array(
            array(
                'field' => 'contact_code',
                'label' => 'Mã khách hàng',
                'rules' => 'required|max_length[50]'
            ),
            array(
                'field' => 'first_name',
                'label' => 'Họ',
                'rules' => 'required|max_length[255]'
            ),
            array(
                'field' => 'last_name',
                'label' => 'Tên',
                'rules' => 'required|max_length[255]'
            ),
        );
        
        $this->form_validation->set_rules($rules);
        if ($this->form_validation->run() == TRUE) {
            $contacts_id = $this->contacts_m->insert($args);

            if (is_numeric($contacts_id) == true) {
                $this->response(array('status' => true, 'data' => $contacts_id), 200);
            } else {
                $this->response(array('status' => false, 'error' => 'Insert error. Please try again!'), 200);
            }
        } else {
            $this->response(array('status' => false, 'error' => validation_errors()), 400);
        }
    }

    public function update() {
        $args = array('id', 'contact_name', 'date_manufacture', 'date_expired', 'manufacturer', 'vendor', 'description', 'contact_code', 'industry', 'category', 'sale_price', 'cost_price');
        $args = $this->_parse_post_args($args);
        //Validation
        $rules = array(
            array(
                'field' => 'id',
                'label' => 'Id',
                'rules' => 'required|is_natural_no_zero'
            ),
            array(
                'field' => 'contact_name',
                'label' => 'Product Name',
                'rules' => 'required|max_length[255]'
            ),
            array(
                'field' => 'date_manufacture',
                'label' => 'Date Manufacture',
                'rules' => 'required|is_natural_no_zero|max_length[11]'
            ),
            array(
                'field' => 'date_expired',
                'label' => 'Date Expired',
                'rules' => 'required|is_natural_no_zero|max_length[11]'
            ),
            array(
                'field' => 'manufacturer',
                'label' => 'Manufacture',
                'rules' => 'required|is_natural_no_zero|max_length[11]'
            ),
            array(
                'field' => 'vendor',
                'label' => 'Vendor',
                'rules' => 'is_natural_no_zero|max_length[11]'
            ),
            array(
                'field' => 'category',
                'label' => 'Category',
                'rules' => 'required|is_natural_no_zero|max_length[11]'
            ),
            array(
                'field' => 'sale_price',
                'label' => 'Sale Price',
                'rules' => 'is_natural_no_zero|max_length[11]'
            ),
            array(
                'field' => 'cost_price',
                'label' => 'Vendor',
                'rules' => 'required|is_natural_no_zero|max_length[11]'
            )
        );
        $this->form_validation->set_rules($rules);
        if ($this->form_validation->run() == TRUE) {
            $id = $args['id'];
            unset($args['id']);
            $result = $this->contacts_m->update($id, $args);
            if ($result == 1) {
                $this->response(array('status' => true, 'data' => $result), 200);
            } else {
                $this->response(array('status' => false, 'error' => 'Update error. Please try again!'), 200);
            }
        } else {
            $this->response(array('status' => false, 'error' => validation_errors()), 400);
        }
    }

    public function delete() {
        $args = array('id');
        //Lấy tham số POST vào Arr
        $args = $this->_parse_post_args($args);
        //Validation
        $rules = array(
            array(
                'field' => 'id',
                'label' => 'Id',
                'rules' => 'required|is_natural_no_zero'
            )
        );
        $this->form_validation->set_rules($rules);
        if ($this->form_validation->run() == TRUE) {
            $result = $this->contacts_m->delete($args['id']);
            if ($result == 1) {
                $this->response(array('status' => true, 'data' => $result), 200);
            } else {
                $this->response(array('status' => false, 'error' => 'Delete error. Please try agian!'), 200);
            }
        } else {
            $this->response(array('status' => false, 'error' => validation_errors()), 400);
        }
    }

    public function getById() {
        $args = array('id');
        //Lấy tham số POST vào Arr
        $args = $this->_parse_post_args($args);
        //Validation
        $rules = array(
            array(
                'field' => 'id',
                'label' => 'Id',
                'rules' => 'required|is_natural_no_zero'
            )
        );
        $this->form_validation->set_rules($rules);
        if ($this->form_validation->run() == TRUE) {
            $contacts = $this->contacts_m->get($args['id']);
            $this->response(array('status' => true, 'data' => $contacts), 200);
        } else {
            $this->response(array('status' => false, 'error' => validation_errors()), 400);
        }
    }

    public function getAll() {
        $contacts = $this->contacts_m->getAll();
        $this->response(array('status' => true, 'data' => $contacts), 200);
    }

}
